<?php
/**
 * 图书章节管理数据模型
 * @author sandy_office
 *
 */
class chapterModel extends Model{
	
	/**
	 * 获取指定图书章节信息
	 */
	public function get_book_chapter($book_id,$page_info = array()){
		$book_id = intval($book_id);
		if (!$book_id) {
			return false;
		}
		
		$return = array('result'=>'','total'=>0);
		
		$sql = "SELECT chapter_id,chapter_title,book_id,sort 
				FROM #__chapter WHERE book_id = $book_id  
				ORDER BY sort DESC, chapter_id DESC";
		
		$sql_count = "	SELECT count(*)  
				FROM #__chapter WHERE book_id = $book_id  
				ORDER BY sort DESC, chapter_id DESC";
		
		if (!empty($page_info)){
			$this->db->set_query($sql,$page_info['offset'],$page_info['limit']);
		} else {
			$this->db->set_query($sql);
		}
		
		$return['result'] = $this->db->get_all();
		
		$this->db->set_query($sql_count);
		$return['total'] = $this->db->get_one();
		
		return $return;
	}
	
	/**
	 * 添加章节
	 */
	public function add_chapter($chapter_info){
		
		$insert_info = array(
			'chapter_title' => $chapter_info['chapter_title'],
			'book_id' => $chapter_info['book_id'],
			'abstract' => $chapter_info['abstract'],
			'sort' => $chapter_info['sort']
		);
		
		return $this->db->insert("#__chapter",$insert_info);
	}
	
	/**
	 * 获取章节信息
	 * 
	 * @param int $chapter_id 
	 */
	public function get_chapter($chapter_id){
		$sql  = "SELECT * FROM #__chapter WHERE chapter_id = $chapter_id";
		$this->db->set_query($sql);
		
		return $this->db->get_row();
	}

	/**
	 * 更新章节信息
	 * @param type $chapter_info 
	 */
	public function update_chapter($chapter_info) {
		$update_info = array(
			'chapter_title' => $chapter_info['chapter_title'],
			'book_id' => $chapter_info['book_id'],
			'abstract' => $chapter_info['abstract'],
			'sort' => $chapter_info['sort']
		);
		
		return $this->db->update('#__chapter',$update_info,'chapter_id = '.$chapter_info['chapter_id']);
	}
	
	/**
	 * 删除章节
	 */
	public function del_chapter(){
		
	}
}